﻿Imports System.IO
Imports System.Xml
Public Class SettingsHelper
    ' 配置文件路径，存储在应用程序目录下的 AppConfig 文件夹中
    Private Shared configFolderPath As String = TaskInfo.configFilePath
    Private Shared configFilePath As String = Path.Combine(configFolderPath, "settings.xml")

    ' 初始化配置文件夹和文件
    Private Shared Sub InitializeConfig()
        If Not Directory.Exists(configFolderPath) Then
            Directory.CreateDirectory(configFolderPath)
        End If

        ' 如果配置文件不存在，则创建一个新的 XML 配置文件
        If Not File.Exists(configFilePath) Then
            Dim settingsDoc As New XmlDocument()
            Dim root As XmlElement = settingsDoc.CreateElement("Settings")
            settingsDoc.AppendChild(root)

            ' 添加默认设置项
            Dim windowA As XmlElement = settingsDoc.CreateElement("WindowState_TaskInfo")
            windowA.InnerText = "True" ' 默认值
            root.AppendChild(windowA)

            Dim windowB As XmlElement = settingsDoc.CreateElement("WindowState_FormCalendar")
            windowB.InnerText = "True" ' 默认值
            root.AppendChild(windowB)

            Dim windowC As XmlElement = settingsDoc.CreateElement("WindowState_Form1")
            windowC.InnerText = "True" ' 默认值
            root.AppendChild(windowC)

            ' 保存到文件
            settingsDoc.Save(configFilePath)
        End If
    End Sub

    ' 获取窗口 A 是否显示
    Public Shared Function ShouldShowWindowA() As Boolean
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowA As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_TaskInfo")
        Return Boolean.Parse(windowA.InnerText)

        'Return My.Settings.WindowState_TaskInfo
    End Function

    ' 设置窗口 A 是否显示
    Public Shared Sub SetWindowAState(isVisible As Boolean)
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowA As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_TaskInfo")
        windowA.InnerText = isVisible.ToString()

        settingsDoc.Save(configFilePath)

        'My.Settings.WindowState_TaskInfo = isVisible
        'My.Settings.Save()
    End Sub

    ' 获取窗口 B 是否显示
    Public Shared Function ShouldShowWindowB() As Boolean
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowB As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_FormCalendar")
        Return Boolean.Parse(windowB.InnerText)

        'Return My.Settings.WindowState_FormCalendar
    End Function

    ' 设置窗口 B 是否显示
    Public Shared Sub SetWindowBState(isVisible As Boolean)
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowB As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_FormCalendar")
        windowB.InnerText = isVisible.ToString()

        settingsDoc.Save(configFilePath)
        'My.Settings.WindowState_FormCalendar = isVisible
        'My.Settings.Save()
    End Sub

    ' 获取窗口 C 是否显示
    Public Shared Function ShouldShowWindowC() As Boolean
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowC As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_Form1")
        Return Boolean.Parse(windowC.InnerText)
        'Return My.Settings.WindowState_Form1
    End Function

    ' 设置窗口 C 是否显示
    Public Shared Sub SetWindowCState(isVisible As Boolean)
        InitializeConfig()

        Dim settingsDoc As New XmlDocument()
        settingsDoc.Load(configFilePath)
        Dim windowC As XmlNode = settingsDoc.SelectSingleNode("//Settings/WindowState_Form1")
        windowC.InnerText = isVisible.ToString()

        settingsDoc.Save(configFilePath)

    End Sub
End Class


